<?php
class CF_App_View_Template extends CF_View_Template implements CF_App_View_Interface
{
	/**
	 * @var string
	 */
	protected $_layoutFolder = 'layouts/';
	
	/**
	 * 
	 * @var string
	 */
	protected $_scriptFolder = 'scripts/';
	
	/**
	 * 
	 * @var string
	 */
	protected $_layout = null;
	
	/**
	 * 
	 * @var string
	 */
	protected $_defaultLayout = 'default';
	
	/**
	 * 
	 * @var string
	 */
	protected $_includeScriptParamName = '__content';
	
	/**
	 * @var boolean
	 */
	protected $_renderLayout = true;
	
	/**
	 * 
	 * @var boolean
	 */
	protected $_renderScript = true;
	
	public function getLayoutFile()
	{
		return $this->_layoutFolder . $this->getLayout();
	}
	
	/**
	 * 
	 */
	public function getScriptFile()
	{
		$controller = CF_App::getInstance()->getControler()->getController();
		$action = CF_App::getInstance()->getControler()->getAction();

		return "$controller/$action";
	}
	
	/**
	 * 
	 * @param string $path
	 * @return string
	 */
	public function setScriptPath($path)
	{
		$this->_scriptPath = $path;
		return $this;
	}
	
	/**
	 * @return string
	 */
	public function getLayout()
	{
		if(null === $this->_layout){
			$this->_layout = $this->_defaultLayout;
		}
		
		return $this->_layout;
	}
	
	/**
	 * 
	 * @param string $layout
	 * @return CF_App_View $this
	 */
	public function setLayout($layout)
	{
		$this->_layout = $layout;
		return $this;
	}
	
	/**
	 * @return boolean
	 */
	public function isRenderLayout()
	{
		return $this->_renderLayout;
	}
	
	/**
	 * 
	 * @param boolean $bol
	 * @return CF_App_View $this
	 */
	public function setRenderLayout($bool)
	{
		$this->_renderLayout = (boolean) $bool;
		return $this;
	}
	
	/**
	 * @return boolean
	 */
	public function isRenderScript()
	{
		return $this->_renderScript;
	}
	
	/**
	 * 
	 * @param boolean $bool
	 * @return CF_App_View_Template $this
	 */
	public function setRenderScript($bool)
	{
		$this->_renderScript = (boolean) $bool;
		return $this;
	}
	
	/**
	 * @return boolean
	 */
	public function isRender()
	{
		return $this->isRenderLayout() || $this->isRenderScript();
	}
	
	/**
	 * 
	 * @param boolean $bool
	 * @return CF_App_View_Template $this
	 */
	public function setRender($bool)
	{
		$this->setRenderLayout($bool);
		$this->setRenderScript($bool);
		
		return $this;
	}
	
	/**
	 * 
	 * @param string $script
	 * @param string $layout [optional]
	 */
	public function render()
	{
		if(!$this->isRender()) return;

		if($this->isRenderLayout()){
			$this->setIncludePath($this->getPath() . $this->_scriptFolder);
			$this->setFile($this->getLayoutFile());
			
			if($this->isRenderScript()){
				$scriptFile = $this->getScriptFile();
				$this->includeFile($this->_includeScriptParamName, $scriptFile);
			}
			return parent::render();
		}
		$this->setPath($this->getPath() . $this->_scriptFolder);
		$this->setFile($this->getScriptFile());
		
		return parent::render();
	}
}